//
//  CZStatusOriginalView.swift
//  微博
//
//  Created by 范承志 on 16/6/11.
//  Copyright © 2016年 fanchengzhi. All rights reserved.
//

import UIKit

class CZStatusOriginalView: UIView {

    //记录原创微博的底部约束
    var originalViewBottomCons: Constraint?
    
    // 定义一个属性 ViewModel 供外界赋值
    var statusViewModel: CZStatusViewModel?{
        didSet{
            // 当前的控件赋值了
            // 头像
            headImageView.CZ_setImage(withUrlString: statusViewModel?.statusModel?.user?.profile_image_url, placeholderImageName: "avatar_default_big")
            // 昵称
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            // 等级
            membershipImageView.image = statusViewModel?.mbrankImage
            // 认证
            avatarImageView.image = statusViewModel?.verifiedImage
            // 微博正文
            contentLabel.text = statusViewModel?.statusModel?.text
            
            sourceLabel.text = statusViewModel?.sourceStr
//            print(statusViewModel?.statusModel?.created_at)
            timeLabel.text = statusViewModel?.timeStr
            
            //配图赋值
            /*
             - 先卸载约束
             - 判断的临界条件 count > 0 就代表有图片
             - 赋值
             - 显示配图
             - 原创微博的底部 == 配图的底部 +10
             - 反之没有图片
             - 隐藏配图
             - 原创微博的底部 == 文字的底部 + 10
             */
            //卸载约束
            originalViewBottomCons?.uninstall()
            //有配图
            if (statusViewModel?.statusModel?.pic_urls?.count)! > 0 {
                
                pictureView.picUrls = statusViewModel?.statusModel?.pic_urls
                pictureView.isHidden = false
                
                self.snp_makeConstraints { (make) in
                    self.originalViewBottomCons = make.bottom.equalTo(pictureView).offset(10).constraint
                }
            } else {
                //没有配图
                pictureView.isHidden = true
                self.snp_makeConstraints { (make) in
                    self.originalViewBottomCons = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //搭建界面
    private func setupUI(){
        
        //添加控件
        //头像
        addSubview(headImageView)
        //昵称
        addSubview(nameLabel)
        //时间
        addSubview(timeLabel)
        //来源
        addSubview(sourceLabel)
        //微博等级
        addSubview(membershipImageView)
        //微博认证
        addSubview(avatarImageView)
        //微博正文
        addSubview(contentLabel)
        //配图
        addSubview(pictureView)
        
        //头像添加约束
        headImageView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 35, height: 35))
            make.top.left.equalTo(10)
        }
        //昵称添加约束
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.left.equalTo(headImageView.snp_right).offset(10)
        }
        //时间添加约束
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        //来源添加约束
        sourceLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(10)
        }
        //微博等级添加约束
        membershipImageView.snp_makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(10)
        }
        //微博认证添加约束
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp_right)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        //微博正文添加约束
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.left.equalTo(headImageView)
        }
        //配图正文添加约束
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.left.equalTo(contentLabel)
        }
        self.snp_makeConstraints { (make) in
            self.originalViewBottomCons = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    }
    
    //懒加载控件
    //1.头像
    private lazy var headImageView: UIImageView = UIImageView(imageName: "")
    
    //2.昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: CZRandomColor(), fontSize: CZNormalFontSize, text: "")
    
    //3.时间
    private lazy var timeLabel: UILabel = UILabel(textColor: CZTHEMECOLOR, fontSize: CZSmallFontSize, text: "时间")
    
    //4.来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: CZTHEMECOLOR, fontSize: CZSmallFontSize, text: "来自乖乖熊的微博")
    
    //5.微博等级
    private lazy var membershipImageView: UIImageView = UIImageView(imageName: "")
    
    //6.认证
    private lazy var avatarImageView: UIImageView = UIImageView(imageName: "")
    
    //7.微博正文
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.black, fontSize: CZNormalFontSize, text: "", maxWidth: CZSCREEN_W - 20)
    
    //8.配图
    private lazy var pictureView: CZStatusPictrueView = {
        
        let view = CZStatusPictrueView()
        
        return view
    }()
}
